package bidsanity

import static org.junit.Assert.*
import org.junit.*
import grails.converters.JSON

class ListingControllerIntegrationTests {
    
    def testListing
    
    @Before
    void setUp() {
        Customer.list()*.delete()
        String domain = "bidsanity.net"

        // Setup logic here
        def tim = new Customer(emailAddress: "Tim@${domain}", password: 'testtest').save()
        def zach = new Customer(emailAddress: "Zach@${domain}", password: 'testtest').save()
        def scott = new Customer(emailAddress: "Scott@${domain}", password: 'testtest').save()

        testListing = new Listing(name: "A Completed Listing", endDate: new Date() - 32, startingBidPrice: 100.0D,
                description: 'completed',seller: zach, winner: tim, startDate: new Date() - 36).save(validate: false)
        for (i in 0..12){
            if(i%2 == 0){
                new Bid(amount: 100.0D + i, bidder: scott, bidTime:  new Date() - (34 - i),
                        listing: testListing).save()
            }
            else{
                new Bid(amount: 100.0D + i, bidder: tim, bidTime:  new Date() - (34 - i),
                        listing: testListing).save()
            }
        }
        testListing.save()
    }

    @After
    void tearDown() {
        // Tear down logic here
        Listing.list()*.delete()
    }

    @Test
    void testTenBidsReturned() {
        def controller = new ListingController()

        Map myParams = [id: testListing.id]
        controller.params.putAll(myParams)
        controller.getBidsForListing()

        def bidRoot = JSON.parse(controller.response.text)
        Assert.assertEquals(2, bidRoot.length())
        def bids = bidRoot.bids
        Assert.assertEquals(10,bids.length())
    }
}
